<?php 

class activityModel extends appModel
{
    
    /**
     * 判断是否已存在相同的金额
     * @param unknown $money
     * @param string $id
     * @return Ambigous <string, boolean>
     */
    public function isExistMoney($money, $id = '')
    {
        $where = " WHERE card_money='".$money."' ";
        if(!empty($id))
        {
            $where .= " AND id<>'".$id."' ";
        }
        $sql = " SELECT COUNT(*) FROM ".$this->table('activity_card').$where;
        return $this->getOne($sql);
    }
    
    /**
     * 插入优惠券信息
     * @param unknown $info
     * @return Ambigous <boolean, resource>
     */
    public function insertCard($info)
    {
        $data = yf_set_insert($info);
        $sql = " INSERT INTO ".$this->table('activity_card')." ( ".$data['key']." ) VALUES( ".$data['val']." ) ";
        return $this->query($sql);
    }
    
    /**
     * 计算总的几率
     * @param unknown $chance
     * @param string $id
     * @return boolean
     */
    public function getAllChance($chance, $id = '')
    {
        $where = " WHERE 1=1 ";
        if(!empty($id))
        {
            $where .= " AND id<>'".$id."' ";
        }
        $sql = " SELECT SUM(card_chance) as allChance FROM ".$this->table('activity_card').$where;
        $allChance = $this->getOne($sql);
        if(($allChance + $chance) > 100)
        {
            return true;
        }
        return false;
    }
    
    /**
     * 获取优惠券列表
     * @param unknown $page
     * @param number $limit
     * @return multitype:string Ambigous <boolean, multitype:multitype: >
     */
    public function getCardList($page, $limit = 10)
    {
        $sql = " SELECT * FROM ".$this->table('activity_card')." ORDER BY ordering DESC ";
        $data = $this->show_page($sql, $page, $limit);
        if($data['result'])
        {
            foreach($data['result'] as $k => $v)
            {
                $data['result'][$k]['used'] = $this->getUsedCount($v['id']);
            }
        }
        return $data;
    }
    
    public function getUsedCount($card_id)
    {
        $sql = " SELECT COUNT(*) FROM ".$this->table('activity_recode')." WHERE card_id='".$card_id."' AND status='1' ";
        return $this->getOne($sql);
    }
    
    /**
     * 修改优惠券的优先级
     * @param unknown $ordering
     * @param unknown $id
     * @return Ambigous <boolean, resource>
     */
    public function orderCard($ordering, $id)
    {
        $sql = " UPDATE ".$this->table('activity_card')." SET ordering='".$ordering."' WHERE id='".$id."' ";
        return $this->query($sql);
    }
    
    /**
     * 获取优惠券信息
     * @param unknown $id
     * @return Ambigous <boolean, multitype:>
     */
    public function getCardInfo($id)
    {
        $sql = " SELECT * FROM ".$this->table('activity_card')." WHERE id='".$id."' ";
        return $this->getRow($sql);
    }
    
    /**
     * 修改优惠券信息
     * @param unknown $info
     * @param unknown $id
     * @return Ambigous <boolean, resource>
     */
    public function updateCard($info, $id)
    {
        $data = yf_set_update($info);
        $sql = " UPDATE ".$this->table('activity_card')." SET ".$data." WHERE id='".$id."' ";
        return $this->query($sql);
    }
    
    /**
     * 删除优惠券
     * @param unknown $id
     * @return Ambigous <boolean, resource>
     */
    public function delCard($id)
    {
        $sql = " DELETE FROM ".$this->table('activity_card')." WHERE id='".$id."' ";
        return $this->query($sql);
    }
    
    /**
     * 获取记录
     * @param unknown $page
     * @param number $limit
     * @return Ambigous <Ambigous, multitype:string Ambigous <boolean, multitype:multitype: > >
     */
    public function getPageLog($page, $limit = 20)
    {
        $sql = " SELECT * FROM ".$this->table('activity_recode')." ORDER BY created DESC ";
        $data = $this->show_page($sql, $page, $limit);
        if($data['result'])
        {
            foreach($data['result'] as $k => $v)
            {
                $data['result'][$k]['created'] = date('Y-m-d H:i', $v['created']);
                $user = $this->getUserInfo($v['wxid'], $v['openid']);
                $data['result'][$k]['nickname'] = $user['nickname'];
                $data['result'][$k]['headimgurl'] = $user['headimgurl'];
            }
        }
        return $data;
    }
    
    /**
     * 获取用户信息
     * @param unknown $id
     * @param unknown $openid
     * @return Ambigous <boolean, multitype:>
     */
    public function getUserInfo($id, $openid)
    {
        $sql = " SELECT nickname, headimgurl FROM ".$this->table('wxuser')." WHERE id='".$id."' AND openid='".$openid."' ";
        return $this->getRow($sql);
    }
    
    /**
     * 获取记录总数
     * @return Ambigous <string, boolean>
     */
    public function getAllRecode()
    {
        $sql = " SELECT COUNT(*) FROM ".$this->table('activity_recode');
        return $this->getOne($sql);
    }
    
    public function getDaoLog($page, $limit = 1000)
    {
        $start = ($page - 1) * $limit;
        $limitSql = "";
        if($page > 0)
        {
            $limitSql = " LIMIT ".$start." , ".$limit;
        }
        $sql = " SELECT * FROM ".$this->table('activity_recode')." ORDER BY created DESC ".$limitSql;
        $data = $this->getAll($sql);
        $arr = array();
        if($data)
        {
            foreach($data as $k => $v)
            {
                $arr[$k]['num'] = $start + 1;
                $user = $this->getUserInfo($v['wxid'], $v['openid']);
                $arr[$k]['openid'] = $v['openid'];
                $arr[$k]['nickname'] = htmlspecialchars($user['nickname']);
                $arr[$k]['money'] = $v['money'];
                $arr[$k]['ip'] = $v['ip'];
                $arr[$k]['created'] = date('Y-m-d H:i');
                $start++;
            }
        }
        return $arr;
    }
    
    /**
     * 获取参与地区列表
     * @return Ambigous <string, Ambigous, boolean, multitype:multitype: >
     */
    public function getAreaLog()
    {
        $sql = " SELECT COUNT(*) AS num, province FROM ".$this->table('activity_recode')." GROUP BY province ORDER BY province DESC ";
        $province = $this->getAll($sql);
        if($province)
        {
            foreach($province as $k => $v)
            {
                if($v['province'] == 0)
                {
                    $province[$k]['name'] = '其他地区';
                }
                else 
                {
                    $province[$k]['name'] = $this->getAreaName($v['province']);
                }
            }
        }
        return $province;    
    }
    
    /**
     * 获取地区名称
     * @param unknown $id
     * @return Ambigous <string, boolean>
     */
    public function getAreaName($id, $pid = '')
    {
        $where = "";
        if(!$pid)
        {
            $where .= " AND pid='".$pid."' ";
        }
        $sql = " SELECT name FROM ".$this->table('areas')." WHERE id='".$id."' ".$where;
        return $this->getOne($sql);
    }
    
    /**
     * 获取参与地区列表2
     * @param unknown $pid
     * @return Ambigous <string, Ambigous, boolean, multitype:multitype: >
     */
    public function getCityLog($pid)
    {
        $sql = " SELECT COUNT(*) AS num, city FROM ".$this->table('activity_recode')." WHERE province='".$pid."' GROUP BY city ORDER BY city DESC ";
        $city = $this->getAll($sql);
        if($city)
        {
            foreach($city as $k => $v)
            {
                if($v['city'] == 0)
                {
                    $city[$k]['name'] = '其他地区';
                }
                else 
                {
                    $city[$k]['name'] = $this->getAreaName($v['city'], $pid);
                }
            }
        }
        return $city;
    }
    
    /**
     * 获取参与人数
     * @return Ambigous <string, boolean>
     */
    public function getJoinNum()
    {
        $sql = " SELECT COUNT(*) FROM ".$this->table('activity_recode');
        return $this->getOne($sql);
    }
    
}